package com.fowo.api.costAccounting.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fowo.api.costAccounting.entity.CarryFcorwardConfig;
import com.fowo.api.costAccounting.entity.MonthlyCostReport;
import com.fowo.api.costAccounting.mapper.CommonCostingMapper;
import com.fowo.api.costAccounting.model.bd.BdAccperiodmonthVo;
import com.fowo.api.costAccounting.model.carry.CarryFcorwardConfigProductVo;
import com.fowo.api.costAccounting.model.monthly.MonthlyCostReportItemVo;
import com.fowo.api.costAccounting.model.stock.StockOutMonthlyVo;
import com.fowo.api.costAccounting.model.stock.StockVo;
import com.fowo.api.costAccounting.service.CarryFcorwardBusinessTableService;
import com.fowo.api.costAccounting.service.CarryFcorwardConfigProductService;
import com.fowo.api.costAccounting.service.CarryFcorwardConfigService;
import com.fowo.api.costAccounting.service.MonthlyCostReportService;
import com.fowo.api.costAccounting.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tech.tablesaw.api.Row;
import tech.tablesaw.api.Table;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CarryFcorwardBusinessTableServiceImpl implements CarryFcorwardBusinessTableService {
    @Resource
    protected CarryFcorwardConfigService carryFcorwardConfigService;
    @Resource
    protected CarryFcorwardConfigProductService carryFcorwardConfigProductService;
    @Resource
    private CommonCostingMapper commonCostingMapper;
    @Resource
    protected MonthlyCostReportService monthlyCostReportService;
    @Override
    public void updateMonthlyCostReport(String serialNumber) throws Exception {
        CarryFcorwardConfig carryFcorwardConfig = carryFcorwardConfigService.getSerialNumber(serialNumber);
        List<CarryFcorwardConfigProductVo> carryFcorwardConfigProductVoList= carryFcorwardConfigProductService.getByCarryFcorwardConfigId(carryFcorwardConfig.getId());
        if(carryFcorwardConfigProductVoList.size()==0){
            throw new Exception("结转子表配置查出没有符合得数据");
        }
        List<BdAccperiodmonthVo> bdAccperiodmonthVoList = commonCostingMapper.getByStatus();
        if (bdAccperiodmonthVoList.size() != 1) {
            throw new Exception("会计月份查出多条或没有符合得数据");
        }
        if (bdAccperiodmonthVoList.get(0).getBeginDate() == null || bdAccperiodmonthVoList.get(0).getEndDate() == null) {
            throw new Exception("会计月份开始时间结束日期未设置");
        }
        //获取当月 处理获取上个月配置的日期查询出
        String ym= bdAccperiodmonthVoList.get(0).getAccperiodMonth();
        if(StringUtils.isBlank(ym)){
            throw new Exception("会计月份未配置");
        }
        //上个月的
        List<BdAccperiodmonthVo> lastbdAccperiodmonthVoList = commonCostingMapper.getByAccperiodMonth(DateUtils.getLastMonth(ym));
        if (lastbdAccperiodmonthVoList.size() != 1) {
            throw new Exception("上个月会计月份查出多条或没有符合得数据");
        }
        if (lastbdAccperiodmonthVoList.get(0).getBeginDate() == null || lastbdAccperiodmonthVoList.get(0).getEndDate() == null) {
            throw new Exception("上个会计月份开始时间结束日期未设置");
        }
        this.businessMonthlyCostReport(carryFcorwardConfig,carryFcorwardConfigProductVoList,bdAccperiodmonthVoList.get(0),lastbdAccperiodmonthVoList.get(0));

    }
    private  void businessMonthlyCostReport(CarryFcorwardConfig carryFcorwardConfig,List<CarryFcorwardConfigProductVo> carryFcorwardConfigProductVoList,BdAccperiodmonthVo bdAccperiodmonthVo,BdAccperiodmonthVo lastBdAccperiodmonthVo) throws Exception{
        Date d=new Date();
        //有可能本月已经结转 删除本月数据  成本月报 即时库存月报
        String updateSql="update inventory_monthly set   del_flag = 1  where period_id="+bdAccperiodmonthVo.getId();
        monthlyCostReportService.deleteByPeriodId(bdAccperiodmonthVo.getId());
        commonCostingMapper.updateSql(updateSql);

        //上月入库数据
        List<StockVo> inLast=new ArrayList<>();
        //上月出库数据
        List<StockOutMonthlyVo> outLast=new ArrayList<>();
        //本月入库数据
        List<StockVo> in=new ArrayList<>();
        for (CarryFcorwardConfigProductVo carryFcorwardConfigProductVo : carryFcorwardConfigProductVoList) {
            String sql=String.format(carryFcorwardConfigProductVo.getSelectSql(),DateUtils.getTimesBeginStr(lastBdAccperiodmonthVo.getBeginDate()),DateUtils.getTimesEndStr(lastBdAccperiodmonthVo.getEndDate()));
            if(1==carryFcorwardConfigProductVo.getType()){
                List<StockVo> stockVosList= commonCostingMapper.selectSqlByCarrForward(sql);
                if(stockVosList.size()>0){
                    inLast.addAll(stockVosList);
                }
                sql=String.format(carryFcorwardConfigProductVo.getSelectSql(),DateUtils.getTimesBeginStr(bdAccperiodmonthVo.getBeginDate()),DateUtils.getTimesEndStr(bdAccperiodmonthVo.getEndDate()));
                List<StockVo> inStockVo= commonCostingMapper.selectSqlByCarrForward(sql);
                if(inStockVo.size()>0){
                    in.addAll(inStockVo);
                }
            }else if(2==carryFcorwardConfigProductVo.getType()){
                List<StockOutMonthlyVo> stockVosList= commonCostingMapper.selectSqlByCarrForwardOut(sql);
                if(stockVosList.size()>0){
                    outLast.addAll(stockVosList);
                }
            }
        }

        //上月的成本月报
        List<MonthlyCostReportItemVo> monthlyCostReportItemVos = monthlyCostReportService.getByModelPeriodId(carryFcorwardConfig.getType().toString(), lastBdAccperiodmonthVo.getId());
        // 大于0的处理里没有的 处理会失败
        if(monthlyCostReportItemVos.size()>0){
            MonthlyCostReportItemVo monthlyCostReportItemVo=new MonthlyCostReportItemVo();
            monthlyCostReportItemVo.setFilterConditions("S01000000000000000000");
            monthlyCostReportItemVo.setOrgId(0L);
            monthlyCostReportItemVo.setItemId(1000000000000000000L);
            monthlyCostReportItemVo.setBeginQty(0);
            monthlyCostReportItemVo.setBeginAmount(new BigDecimal(0));
            if(carryFcorwardConfig.getType()==2){
                monthlyCostReportItemVo.setStockId(1000000000000000000L);
            }else if(carryFcorwardConfig.getType()==3){
                monthlyCostReportItemVo.setShopId("0");
            }
            monthlyCostReportItemVos.add(monthlyCostReportItemVo);
            String monthlyCostReportItemString = JSON.toJSONString(monthlyCostReportItemVos);
            Table table = Table.read().string(monthlyCostReportItemString, "json");
            Table result=table;
            // System.out.println(result.printAll());
            //入库 出库可能会有 只有五位数的物料 手动添加一条 处理
            if(inLast.size()>0){
                StockVo stockVo=new StockVo();
                stockVo.setInFilterConditions("S01000000000000000000");
                stockVo.setOrgId(0L);
                stockVo.setItemId(1000000000000000000L);
                stockVo.setQty(0);
                stockVo.setAmount(new BigDecimal(0));
                if(carryFcorwardConfig.getType()==2){
                    stockVo.setStockId(1000000000000000000L);
                }else if(carryFcorwardConfig.getType()==3){
                    stockVo.setShopId("0");
                }
                inLast.add(stockVo);
                String inLastString = JSON.toJSONString(inLast);
                Table tableIn = Table.read().string(inLastString, "json");
                //  System.out.println(tableIn.printAll());
                result= result(carryFcorwardConfig,table,tableIn);
            }
            if(outLast.size()>0){
                StockOutMonthlyVo stockOutMonthlyVo=new StockOutMonthlyVo();
                stockOutMonthlyVo.setOrgId(0L);
                stockOutMonthlyVo.setItemId(1000000000000000000L);
                stockOutMonthlyVo.setOutQty(0);
                stockOutMonthlyVo.setOutAmount(new BigDecimal(0));
                if(carryFcorwardConfig.getType()==2){
                    stockOutMonthlyVo.setStockId(1000000000000000000L);
                }else if(carryFcorwardConfig.getType()==3){
                    stockOutMonthlyVo.setShopId("0");
                }
                outLast.add(stockOutMonthlyVo);
                String outLastString = JSON.toJSONString(outLast);
                Table tableOut = Table.read().string(outLastString, "json");
                result= result(carryFcorwardConfig,result,tableOut);
            }
            //数据重新封装之前的数据 修改  期末作为期初添加 本月入库数据比较数据 没有的添加
            List<MonthlyCostReportItemVo> updateCostReportList=new ArrayList<>();
            List<MonthlyCostReport> insertCostReportList=new ArrayList<>();
            result.forEach(t->{
                JSONObject map= this.rowMaps(t);
                MonthlyCostReportItemVo updateCostRepor=JSONObject.parseObject(JSON.toJSONString(map), MonthlyCostReportItemVo.class);
                MonthlyCostReport insertCostReport=new MonthlyCostReport();
                BeanUtils.copyProperties(updateCostRepor, insertCostReport);
                //入库
                BigDecimal inAmount= this.amount(t,"amount");
                Integer inQty=this.qty(t,"qty");
                //出库
                BigDecimal outAmount=this.amount(t,"outAmount");
                Integer outQty=this.qty(t,"outQty");
                updateCostRepor.setStockInQty(inQty);
                updateCostRepor.setStockInAmount(inAmount);
                updateCostRepor.setStockOutQty(outQty);
                updateCostRepor.setStockOutAmount(outAmount);

                updateCostRepor.setStockAmount(updateCostRepor.getBeginAmount().add(updateCostRepor.getStockInAmount()).subtract(updateCostRepor.getStockOutAmount()));
                updateCostRepor.setStockQty(updateCostRepor.getBeginQty()+updateCostRepor.getStockInQty()-updateCostRepor.getStockOutQty());
                insertCostReport.setPeriodId(bdAccperiodmonthVo.getId());
                insertCostReport.setBeginAmount(updateCostRepor.getStockAmount());
                insertCostReport.setBeginQty(updateCostRepor.getStockQty());
                insertCostReport.setStockInAmount(new BigDecimal(0));
                insertCostReport.setStockInQty(0);
                insertCostReport.setStockOutQty(0);
                insertCostReport.setStockOutAmount(new BigDecimal(0));
                insertCostReport.setStockQty(0);
                insertCostReport.setStockAmount(new BigDecimal(0));
                insertCostReport.setId(null);
                updateCostReportList.add(updateCostRepor);
                insertCostReportList.add(insertCostReport);
            });
            //根据条件 修改上月的
            monthlyCostReportService.updateBatch(updateCostReportList,carryFcorwardConfig.getType().toString());
//       //添加的 期初数据
            monthlyCostReportService.insertBatch(insertCostReportList);
        }
        //处理入库 比较成本月报 数据 没有的插入  两个数据比较 长度大于0 校验下
        // //太慢了 直接查询的时候 直接把条件组装 过滤
        if(in.size()>0){
            StockVo stockVo=new StockVo();
            stockVo.setInFilterConditions("S01000000000000000000");
            stockVo.setOrgId(0L);
            stockVo.setItemId(1000000000000000000L);
            stockVo.setQty(0);
            stockVo.setAmount(new BigDecimal(0));
            if(carryFcorwardConfig.getType()==2){
                stockVo.setStockId(1000000000000000000L);
            }else if(carryFcorwardConfig.getType()==3){
                stockVo.setShopId("0");
            }
            in.add(stockVo);
            if(monthlyCostReportItemVos.size()>0){
                List<String> filterConditions = monthlyCostReportItemVos.stream().map(MonthlyCostReportItemVo::getFilterConditions).collect(Collectors.toList());
                in = in.stream().filter(t -> !filterConditions.contains(t.getInFilterConditions())).collect(Collectors.toList());
//               String monthlyCostReportItemString = JSON.toJSONString(monthlyCostReportItemVos);
//               Table table = Table.read().string(monthlyCostReportItemString, "json");
//               String inString = JSON.toJSONString(in);
//               Table tableIn = Table.read().string(inString, "json");
//               List<StockVo> insertStockVo=new ArrayList<>();
//               tableIn.forEach(item->{
//                   AtomicReference<Boolean> b= new AtomicReference<>(false);
//                   table.forEach(item1->{
//                       if(carryFcorwardConfig.getType()==1){
//                           if(item.getObject("itemId").equals(item1.getObject("itemId"))&&item.getObject("orgId").equals(item1.getObject("orgId"))){
//                               b.set(true);
//                               return;
//                           }
//                       }else if(carryFcorwardConfig.getType()==2){
//                           if(item.getObject("itemId").equals(item1.getObject("itemId"))&&item.getObject("orgId").equals(item1.getObject("orgId"))&&item.getObject("stockId").equals(item1.getObject("stockId"))) {
//                               b.set(true);
//                               return;
//                           }
//                       }else if(carryFcorwardConfig.getType()==3){
//                             if(item.getObject("itemId").equals(item1.getObject("itemId"))&&item.getObject("orgId").equals(item1.getObject("orgId"))&&item.getObject("shopId").equals(item1.getObject("shopId"))) {
//                                b.set(true);
//                                return;
//                             }
//                       }
//                   });
//                   if(b.get()==false){
//                       JSONObject map= this.rowMaps(item);
//                       StockVo stockVo=JSONObject.parseObject(JSON.toJSONString(map), StockVo.class);
//                       insertStockVo.add(stockVo);
//
//                   }
//               });
                if(in.size()>0){
                    monthlyCostReportService.saveInBatch(in,bdAccperiodmonthVo.getId());
                }
            }else {
                //直接添加
                monthlyCostReportService.saveInBatch(in,bdAccperiodmonthVo.getId());
            }

        }
        //copy 过去
        monthlyCostReportService.insertCopys(bdAccperiodmonthVo.getId());
        Date end =new Date();
        System.out.println(end.getTime()-d.getTime());
    }
    private  JSONObject rowMaps(Row row){
        JSONObject map=new JSONObject();
        List<String> list=row.columnNames();
        list.forEach(
                item->{
                    map.put(item, this.row(row,item)) ;
                }
        );
        return map;

    }
    private   Object row(Row row, String params){
        try {
            return row.getObject(params);
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
    }
    private  BigDecimal  amount(Row row,String params){
        try {
            return new BigDecimal(row.getObject(params).toString());
        }catch (Exception e){
            return new BigDecimal(0);
        }
    }
    private  Integer  qty(Row row,String params){
        try {
            return Integer.parseInt(row.getObject(params).toString());
        }catch (Exception e){
            return 0;
        }
    }
    //
    private Table result(CarryFcorwardConfig carryFcorwardConfig, Table table, Table table1) throws Exception{
        switch (carryFcorwardConfig.getType()) {
            case 1:
                String [] s={"itemId","orgId"};
                return  table.joinOn(s).leftOuter(table1, s);
            case 2:
                String [] s1={"itemId","orgId","stockId"};
                return  table.joinOn(s1).leftOuter(table1, s1);
            case 3:
                String [] s2={"itemId","orgId","shopId"};
                return  table.joinOn(s2).leftOuter(table1, s2);
            default:
                throw new Exception("对应模式不符合");
        }
    }


    @Override
    public void reverseMonthlyCostReport() throws Exception {
        List<BdAccperiodmonthVo> bdAccperiodmonthVoList = commonCostingMapper.getByStatus();
        if (bdAccperiodmonthVoList.size() != 1) {
            throw new Exception("会计月份查出多条或没有符合得数据");
        }
        //删除操作
        monthlyCostReportService.deleteByPeriodId(bdAccperiodmonthVoList.get(0).getId());
    }

    @Override
    public void newUpdateMonthlyCostReport() throws Exception {
        List<BdAccperiodmonthVo> bdAccperiodmonthVoList = commonCostingMapper.getByStatus();
        if (bdAccperiodmonthVoList.size() != 1) {
            throw new Exception("会计月份查出多条或没有符合得数据");
        }
        String skuListsql=" SELECT\n" +
                "  0 orgId,      product_id itemId,\n" +
                "        IFNULL(price,0) amount\n" +
                "        FROM\n" +
                "        sku_price\n" +
                "      where product_id is not null";
        //查询对应sku_price 和入库数据
        List<Map<String,Object>> SKUlist= commonCostingMapper.selectSqlMaps(skuListsql);
//         String sql="SELECT \n" +
//                 "\t      0 orgId,\n" +
//                 "\t     p.product_id  itemId,\n" +
//                 "   p.unit_price+IFNULL(p.accessories_price,0)  amount1 from \t stock_in_product p\n" +
//                 "\t    LEFT JOIN stock_in s ON s.id = p.parent_stock_in_id \n" +
//                 "\t\t\tINNER JOIN (\t\t\tSELECT\n" +
//                 "\t\t\t\t\ts.id,\n" +
//                 "\t\t\t\t\tmax(s.warehousing_date) warehousing_date,\n" +
//                 "\t      0 orgId,\n" +
//                 "\t     p.product_id  itemId,\n" +
//                 "\t\t\t\tp.unit_price\n" +
//                 "      FROM\n" +
//                 "\t    stock_in_product p\n" +
//                 "\t    LEFT JOIN stock_in s ON s.id = p.parent_stock_in_id\n" +
//                 "     WHERE\n" +
//                 "\t    s.del_flag = 0\n" +
//                 "\t    AND p.del_flag = 0 \n" +
//                 "\t\t\tand s.status=1\n" +
//                 "\t\t\tGROUP BY p.product_id\n" +
//                 "\t\t\tORDER BY p.product_id ) t  on t.itemId=p.product_id and t.warehousing_date=s.warehousing_date\n" +
//                 "\t\t\tWHERE\n" +
//                 "\t    s.del_flag = 0\n" +
//                 "\t    AND p.del_flag = 0 \n" +
//                 "\t\t\tand s.status=1 \n" +
//                 "\t\t\tGROUP BY p.product_id \n" +
//                 "\t\t\tORDER BY  p.product_id";

        String sql="SELECT \n" +
                "\t      0 orgId,\n" +
                "\t     p.product_id  itemId,\n" +
                "   p.unit_price+IFNULL(p.accessories_price,0)  amount1 from \t stock_in_product p\n" +
                "\t    LEFT JOIN stock_in s ON s.id = p.parent_stock_in_id \n" +
                "\t\t\tINNER JOIN  (\t\t\tSELECT\n" +
                "\t\t\t\t\ts.id,\n" +
                "\t\t\t\t\tmax(s.warehousing_date) warehousing_date,\n" +
                "\t      0 orgId,\n" +
                "\t     p.product_id  itemId,\n" +
                "\t\t\t\tp.unit_price\n" +
                "      FROM\n" +
                "\t    stock_in_product p\n" +
                "\t    LEFT JOIN stock_in s ON s.id = p.parent_stock_in_id\n" +
                "     WHERE\n" +
                "\t    s.del_flag = 0\n" +
                "\t    AND p.del_flag = 0 \n" +
                " and s.status=1  and  s.warehousing_date  BETWEEN '2023-06-01 00:00:00' and '"+DateUtils.getTimesEndStr(bdAccperiodmonthVoList.get(0).getEndDate())+"' \n" +
                " GROUP BY p.product_id   \n" +
                "\t\t\tORDER BY p.product_id  ) t  on t.itemId=p.product_id and t.warehousing_date=s.warehousing_date\n" +
                "\t\t\tWHERE\n" +
                "\t    s.del_flag = 0\n" +
                "\t    AND p.del_flag = 0 \n" +
                "\t\t\tand s.status=1  and  s.warehousing_date  BETWEEN '2023-06-01 00:00:00' and '"+DateUtils.getTimesEndStr(bdAccperiodmonthVoList.get(0).getEndDate())+"' \n" +
                "\t\t\tGROUP BY p.product_id  " +
                " ORDER BY  p.product_id";
        List<Map<String,Object>> in= commonCostingMapper.selectSqlMaps(sql);
        Table tablein = Table.read().string(JSON.toJSONString(in), "json");
        Table sku = Table.read().string(JSON.toJSONString(SKUlist), "json");
        String [] s={"itemId","orgId"};
        Table inner= sku.joinOn(s).leftOuter(tablein, s);

        List<Object> filterConditions = SKUlist.stream().map(item->item.get("itemId")).collect(Collectors.toList());
        in = in.stream().filter(t -> !filterConditions.contains(t.get("itemId"))).collect(Collectors.toList());
        List<MonthlyCostReport> insertCostReportList=new ArrayList<>();
        List<String> updateSqlList=new ArrayList<>();
        inner.forEach(item->{
            MonthlyCostReport MonthlyCostReport=new MonthlyCostReport();
            MonthlyCostReport.setItemId(item.getLong("itemId"));
            if(item.getObject("amount1")==null){
                MonthlyCostReport.setCurrentIssueCostAmount(new BigDecimal(item.getObject("amount").toString()));
            }else {
                MonthlyCostReport.setCurrentIssueCostAmount(new BigDecimal(item.getObject("amount1").toString()));
            }
            MonthlyCostReport.setPeriodId(bdAccperiodmonthVoList.get(0).getId());
            MonthlyCostReport.setOrgId(0L);
            insertCostReportList.add(MonthlyCostReport);
            String updateSql="  update    sales_outbound s\n" +
                    "        set s.current_issue_cost_amount=s.quantity*%s, s.cost_unit_price=%s" +
                    "        where  s.del_flag = 0 " +
                    "        AND   s.posted_date_locale  BETWEEN  '%s'  and '%s'\n" +
                    "        and  s.product_id=%s";
            updateSql=String.format(updateSql,MonthlyCostReport.getCurrentIssueCostAmount(),MonthlyCostReport.getCurrentIssueCostAmount(), DateUtils.getTimesBeginStr( bdAccperiodmonthVoList.get(0).getBeginDate()),DateUtils.getTimesEndStr(bdAccperiodmonthVoList.get(0).getEndDate()),MonthlyCostReport.getItemId());
            updateSqlList.add(updateSql);
        });


        Table inIsert = Table.read().string(JSON.toJSONString(in), "json");
        inIsert.forEach(item->{
            MonthlyCostReport MonthlyCostReport=new MonthlyCostReport();
            MonthlyCostReport.setItemId(item.getLong("itemId"));
            MonthlyCostReport.setCurrentIssueCostAmount(new BigDecimal(item.getObject("amount1").toString()));
            MonthlyCostReport.setPeriodId(bdAccperiodmonthVoList.get(0).getId());
            MonthlyCostReport.setOrgId(0L);
            insertCostReportList.add(MonthlyCostReport);
            String updateSql="  update    sales_outbound s\n" +
                    "        set s.current_issue_cost_amount=s.quantity*%s, s.cost_unit_price=%s" +
                    "        where  s.del_flag = 0 " +
                    "        AND   s.posted_date_locale  BETWEEN  '%s'  and '%s'\n" +
                    "        and  s.product_id=%s";
            updateSql=String.format(updateSql,MonthlyCostReport.getCurrentIssueCostAmount(), MonthlyCostReport.getCurrentIssueCostAmount(),DateUtils.getTimesBeginStr( bdAccperiodmonthVoList.get(0).getBeginDate()),DateUtils.getTimesEndStr(bdAccperiodmonthVoList.get(0).getEndDate()),MonthlyCostReport.getItemId());
            updateSqlList.add(updateSql);
        });
        System.out.println(1);
        int listSize = updateSqlList.size();
        int fetchSize = 1000; //900条执行一IC  in orcale最大查询1000
        for (int i = 0; i < updateSqlList.size(); i += fetchSize) {
            try {
                if (i + fetchSize > listSize) {        //作用为toIndex最后没有100条数据则剩余几条newList中就装几条
                    fetchSize = listSize - i;
                }
                List<String> list = updateSqlList.subList(i, i + fetchSize);
                commonCostingMapper.updateSqlList(list);
            } catch (Exception e) {
                log.info("处理失败 ", e);
            }
        }
//
//        //有可能本月已经结转 删除本月数据  成本月报 即时库存月报
        String updateSql="update inventory_monthly set  del_flag = 1  where period_id="+bdAccperiodmonthVoList.get(0).getId();
        monthlyCostReportService.deleteByPeriodId(bdAccperiodmonthVoList.get(0).getId());
        commonCostingMapper.updateSql(updateSql);
        //       //添加的 期初数据 只有物料 成本金额字段
        monthlyCostReportService.insertBatch(insertCostReportList);
        monthlyCostReportService.insertCopys(bdAccperiodmonthVoList.get(0).getId());
    }
}
